<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="bkId" label="技术员">
        <el-select style="width: 300px" v-model="dataForm.bkId" filterable remote reserve-keyword placeholder="输入手机号或姓名" :remote-method="getUserList" :loading="loading" @change="changeUser">
          <el-option v-for="item in userList" :key="item.value" :label="item.username + '-' + item.mobile" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="dataForm.machineType == 1" label="预约时间">
        <el-col :span="4">
          <el-button @click="addAppointHandle(0)">
            <el-icon>
              <Clock />
            </el-icon>
            选择
          </el-button>
          <order-update-tech-time ref="orderTimeRef" @custom-event="handleCustomEvent"></order-update-tech-time>
        </el-col>
        <el-col :span="8">
          <el-text v-for="time in dataForm.bookAllTimes.split(',')" class="mx-1" type="primary">
            <el-icon>
              <Clock />
            </el-icon>
            {{ time }} <br />
          </el-text>
        </el-col>
        <el-col :span="2" style="text-align: right; margin-right: 10px">工时</el-col>
        <el-col :span="8" v-if="dataForm.bookTotalTime">
          <el-text class="mx-1" type="primary">
            {{ dataForm.bookTotalTime }}
          </el-text>
        </el-col>
      </el-form-item>
      <el-form-item v-else label="样品数量">
        <el-text class="mx-1" type="primary">
          {{ dataForm.bookTotalTime }}
        </el-text>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import OrderUpdateTechTime from "@/views/yckj/order-update-tech-time.vue";

const loading = ref(false);

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const userList = ref<any[]>([]);

const dataForm = reactive({
  id: "",
  bkId: "",
  bkName: "",
  machineType: 1,
  bookAllTimes: "",
  bookTotalTime: 0
});

const rules = ref({
  bkId: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  bkName: [{ required: true, message: "必填项不能为空", trigger: "blur" }]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }
  //获取项目列表
  Promise.all([getUserList("")]).then(() => {
    if (id) {
      getInfo(id);
    }
  });
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/yckj/order/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};
// 获取用户列表
const getUserList = async (query: string) => {
  loading.value = true;
  const res = await baseService.get("/sys/user/listTech", { key: query });
  userList.value = res.data;
  loading.value = false;
};

const changeUser = (value: number) => {
  const selectedItem = userList.value.find((item) => item.id === value);
  dataForm.bkName = selectedItem.username;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/yckj/order/updateTech", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

const handleCustomEvent = (value: any) => {
  dataForm.bookAllTimes = value.toString();
  dataForm.bookTotalTime = value.length / 2;
};

const orderTimeRef = ref();
const addAppointHandle = (machineId: any) => {
  orderTimeRef.value.addAppoint(machineId);
};
defineExpose({
  init
});
</script>
